<?php
/**
 * Created by PhpStorm.
 * User: wxy
 * Date: 2017/11/1
 * Time: 11:32
 */

namespace frontend\modules\common\models;

use yii\helpers\Html;
use yii\helpers\Url;
use Yii;
use yii\grid\ActionColumn;

class ActionJSColumn extends ActionColumn
{
    /**
     * Initializes the default button rendering callbacks.
     */
    protected function initDefaultButtons()
    {
        $this->initDefaultButton('update', 'pencil', 'update');
        $this->initDefaultButton('delete', 'trash', 'delete');
        //banner管理
        $this->initDefaultButton('banner', 'pencil', 'banner');
        //用户管理
        $this->initDefaultButton('users', 'pencil', 'users');
        //分类管理
        $this->initDefaultButton('branch-classes', 'pencil', 'branch-classes');
        $this->initDefaultButton('branch-resource', 'pencil', 'branch-resource');
        //推荐资源 首页资源
        $this->initDefaultButton('support', 'pencil', 'support');
        $this->initDefaultButton('top-support', 'pencil', 'top-support');

        //专题列表 special
        $this->initDefaultButton('special', 'pencil', 'special');

        //专题资源列表 special-resource
        $this->initDefaultButton('special-resource', 'pencil', 'special-resource');

        //banner 显示隐藏
        $this->initDefaultButton('show', 'pencil', 'show');

        //用户 冻结 解冻
        $this->initDefaultButton('user-status', 'pencil', 'user-status');
        //分类  显示  隐藏
        $this->initDefaultButton('cate-status', 'pencil', 'cate-status');
        //查看详情
        $this->initDefaultButton('detail', 'pencil', 'detail');

        //后台账户管理
        $this->initDefaultButton('active', 'pencil', 'active');
        //专题推荐support
        $this->initDefaultButton('sp', 'pencil', 'sp');

    }

    //$model->userStatus->status  btn-success
    protected function initDefaultButton($name, $iconName, $additionalOptions = [])
    {
        if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
            $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {

                $btnClass = 'btn-primary';
                switch ($name) {
                    case 'update':
                        $title = '修改';
                        break;
                    case 'banner':
                        $title = 'Banner管理';
                        break;
                    case 'branch-resource':
                        $title = '资源管理';
                        break;
                    case 'branch-classes':
                        $title = '分类管理';
                        break;
                    case 'special':
                        $title = '专题列表';
                        break;
                    case 'special-resource':
                        $title = '专题资源列表';
                        break;
                    case 'support':
                        $title = '推荐资源';
                        break;
                    case 'top-support':
                        $title = '首页资源';
                        break;
                    case 'active':
                        if ($model->active == 0) {
                            $title = '解冻';
                            $btnClass = 'btn-success';

                        } else {
                            $title = '冻结';
                            $btnClass = 'btn-danger';
                        }
                        break;
                    case 'sp':
                        if ($model->support == 0) {
                            $title = '推荐';
                            $btnClass = 'btn-success';

                        } else {
                            $title = '不推荐';
                            $btnClass = 'btn-warning';
                        }
                        break;
                    case 'show':
                        if ($model->show == 1) {
                            $title = '隐藏';
                            $btnClass = 'btn-danger';

                        } else {
                            $title = '显示';
                            $btnClass = 'btn-success';
                        }
                        break;
                    case 'detail':
                        $title = '详情';
                        $btnClass = 'btn-info';
                        break;
                    case 'delete':
                        $title = '删除';
                        $btnClass = 'btn-danger';
                        break;
                    case 'user-status':
                        if ($model->userStatus->status == 1) {
                            $title = '冻结';
                            $btnClass = 'btn-danger';
                        } else {
                            $title = '解冻';
                            $btnClass = 'btn-success';
                        }
                        break;
                    case 'cate-status':
                        if ($model->status == 1) {
                            $title = '隐藏';
                            $btnClass = 'btn-danger';

                        } else {
                            $title = '显示';
                            $btnClass = 'btn-success';
                        }
                        break;
                    case 'users':
                        $title = '用户管理';
                        break;
                    default:
                        $title = ucfirst($name);
                }
                $icon = Html::tag('button', $title, ['class' => "btn  btn-sm $btnClass cao-zuo $additionalOptions"]);
//                //特殊
//                if($name == 'detail'){
//                    $title = '详情';
//                    $btnClass = 'btn-info';
//                    $icon = Html::tag('button', $title, [// 'data-toggle' => 'modal',
//                        'class' => "btn  btn-sm $btnClass cao-zuo $additionalOptions",
//                        'data-toggle' => 'modal',
//                        'data-target' => '#view-modal',
//                        'data-id' => $model->id,
//                    ]);
//                }
                $options = array_merge([
                    'title' => $title,
                    'aria-label' => $title,
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a($icon, $url, $options);
            };
        }
    }

    /**
     * 重写了标签渲染方法。
     * @param mixed $model
     * @param mixed $key
     * @param int $index
     * @return mixed
     */
    protected function renderDataCellContent($model, $key, $index)
    {
        return preg_replace_callback('/\\{([^}]+)\\}/', function ($matches) use ($model, $key, $index) {
            $name = $matches[1];

            return call_user_func($this->buttons[$name], 'javascript:;', $model, $key);
        }, $this->template);

    }

}